﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Einstein.Data;
using DevComponents.Editors;
using DevComponents.AdvTree; 
using Einstein.Utility;
using DevComponents.DotNetBar;
using Einstein.Group.DAL;

namespace Einstein.Platform.Window.Basic
{
    public partial class Basic_Org : Office2007Form
    {
        public Basic_Org()
        {
            InitializeComponent();
            BindOrgTree();
        }

        void BindOrgTree()
        { 
            advTree1.Nodes.Clear();
            IList<DeptInfo> list = new DeptInfoDAL().SelectAll();
            if (list != null)
            {
                foreach (DeptInfo item in list.Where(a => a.ParentID == 0))
                {

                    Node node = new Node { Text = item.DeptName, DataKey = item.DeptID, Expanded = true, Tag = item };
                    advTree1.Nodes.Add(node);
                    IList<DeptInfo> child = list.Where(a => a.ParentID == item.DeptID).ToList();
                    if (child != null)
                    {
                        AddTree(child, list, node);
                    }
                }
            }
            Basic_Org_Edit.Enabled = false;
            Basic_Org_Delete.Enabled = false;
        }

        void AddTree(IList<DeptInfo> childList, IList<DeptInfo> mainList, Node node)
        {
            foreach (DeptInfo item in childList)
            {
                Node childNode = new Node { Text = item.DeptName, DataKey = item.DeptID, Expanded = true, Tag = item };
                node.Nodes.Add(childNode);
                IList<DeptInfo> child = mainList.Where(a => a.ParentID == item.DeptID).ToList();
                if (child != null)
                {
                    AddTree(child, mainList, childNode);
                }
            }
        }

        private void Basic_Org_New_Click(object sender, EventArgs e)
        {
            Basic_Org_New form = new Basic.Basic_Org_New();
            form.Text = "新建组织";
            if (form.ShowDialog() == DialogResult.OK)
            {
                BindOrgTree();
            }
        }


        DeptInfo currentData;
        public DeptInfo CurrentData
        {
            get
            {
                return currentData;
            }
            set
            {
                Basic_Org_Edit.Enabled = true;
                Basic_Org_Delete.Enabled = true;
                currentData = value;
            }
        }


        private void advTree1_AfterNodeSelect(object sender, AdvTreeNodeEventArgs e)
        {
            if (e.Node != null)
            {
                DeptInfo currentItem = e.Node.Tag as DeptInfo;
                if (currentItem != null)
                {
                    CurrentData = currentItem;
                }
            }
        }

        private void Basic_Org_Edit_Click(object sender, EventArgs e)
        {
            if (CurrentData != null)
            {
                Basic_Org_New form = new Basic.Basic_Org_New();
                form.Text = "编辑组织";
                form.DeptInfo = CurrentData;
                form.BindData();
                if (form.ShowDialog() == DialogResult.OK)
                {
                    BindOrgTree();
                }
            }
            else
            {
                BoxManager.ShowWarning("您没有选择任何行，请重试。");
            }
        }

        private void Basic_Org_Delete_Click(object sender, EventArgs e)
        {
            if (BoxManager.ShowDeleteAsk() != DialogResult.OK)
            {
                return;
            }

            if (CurrentData != null)
            {

                bool result = new DeptInfoDAL().Delete(new DeptInfo { DeptID = CurrentData.DeptID });
                if (result)
                {
                    BindOrgTree();
                    BoxManager.ShowDeleteSuccess();
                }
                else
                {
                    BoxManager.ShowDeleteFail();
                }
            }
            else
            {
                BoxManager.ShowWarning("您没有选择任何行，请重试。");
            }
        }
         
    }
}
